package com.ilop.sthome.utils;

import android.content.Context;
import android.content.res.AssetManager;

import com.alibaba.fastjson.JSON;
import com.ilop.sthome.data.bean.AlarmNotice;
import com.ilop.sthome.data.bean.VirtualUserBean;
import com.ilop.sthome.data.greenDao.DeviceInfoBean;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

/**
 * @author skygge
 * @date 2020/6/18.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：解析Json数据
 */
public class ParsingJsonUtil {

    public static volatile ParsingJsonUtil instance;

    public static ParsingJsonUtil getInstance() {
        if (instance == null){
            synchronized (ParsingJsonUtil.class){
                instance = new ParsingJsonUtil();
            }
        }
        return instance;
    }

    /**
     * 读取assets本地json
     * @param fileName 文件名称
     * @return
     */
    public String getJson(Context context, String fileName) {
        //将json数据变成字符串
        StringBuilder stringBuilder = new StringBuilder();
        try {
            //获取assets资源管理器
            AssetManager assetManager = context.getAssets();
            //通过管理器打开文件并读取
            BufferedReader bf = new BufferedReader(new InputStreamReader(
                    assetManager.open(fileName)));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }

    /**
     * 获取网关列表
     * @param data 接口返回数据
     * @return 网关列表
     */
    public List<DeviceInfoBean> getGatewayList(Object data){
        try {
            JSONObject jsonObject = (JSONObject) data;
            JSONArray jsonArray = jsonObject.getJSONArray("data");
            return JSON.parseArray(jsonArray.toString(), DeviceInfoBean.class);
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取房间和摄像头列表
     * @param data 接口返回数据
     * @return 房间和摄像头列表
     */
    public List<VirtualUserBean> getRoomAndCameraList(Object data){
        try {
            JSONObject jsonObject = (JSONObject) data;
            JSONArray jsonArray = jsonObject.getJSONArray("data");
            return JSON.parseArray(jsonArray.toString(), VirtualUserBean.class);
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取网关在线离线推送
     * @param data 接口返回数据
     * @return 网关在线离线推送使能
     */
    public List<AlarmNotice> getAlarmNotice(Object data){
        if (data instanceof JSONArray) {
            try {
                return JSON.parseArray(data.toString(), AlarmNotice.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
